<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleAudit({})" v-auth="'shop:apply:soReturnApply:sp'">
          <Icon icon="simple-line-icons:check" /> {{ t('审核') }}
        </a-button>
        <a-popconfirm title="是否确认反审？" ok-text="是" cancel-text="否" @confirm="handleUnSp()">
          <a-button type="error" v-auth="'shop:apply:soReturnApply:sp'">
            <Icon icon="simple-line-icons:close" /> {{ t('反审') }}
          </a-button>
        </a-popconfirm>
        <a-button type="primary" @click="handleApply()" v-auth="'shop:apply:soReturnApply:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <!-- 退货 -->
    <BasicModal v-bind="$attrs" @register="registerModal" title="新增退货订单" @ok="handleModal">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopApplySoReturnApplyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    soReturnApplyDelete,
    soReturnApplyListData,
    auditOrder,
    batchDeSp,
  } from '/@/api/shop/apply/soReturnApply';
  import { basUserCusTreeData } from '/@/api/bas/cus/basUserCus';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';

  const { t } = useI18n('shop.apply.soReturnApply');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('退货申请单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('单据号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('单据日期'),
        field: 'ddate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('业务类型'),
        field: 'cbustype',
        component: 'Select',
        componentProps: {
          dictType: 'bus_type',
          allowClear: true,
        },
      },
      {
        label: t('客户编码'),
        field: 'cusCode',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'basCus.name',
        component: 'Input',
      },
      {
        label: t('核发店面'),
        field: 'hfdmCode',
        component: 'Input',
      },
      {
        label: t('是否首批'),
        field: 'bsp',
        component: 'Select',
        componentProps: {
          dictType: 'bus_is_first',
          allowClear: true,
        },
      },
      {
        label: t('物流公司'),
        field: 'wlComp',
        component: 'Input',
      },
      {
        label: t('物流单号'),
        field: 'wlNo',
        component: 'Input',
      },
      {
        label: t('审核人'),
        field: 'auditor',
        component: 'Input',
      },
      {
        label: t('审核日期'),
        field: 'auditDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('审核状态'),
        field: 'auditStatus',
        component: 'Select',
        componentProps: {
          dictType: 'so_return_audit_status',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('状态'),
      dataIndex: 'auditStatus',
      dictType: 'so_return_audit_status',
      key: 'a.audit_status',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('单据号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 200,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('单据日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('业务类型'),
      dataIndex: 'cbustype',
      key: 'a.cbustype',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'bus_type',
    },
    {
      title: t('客户编码'),
      dataIndex: 'cusCode',
      key: 'a.cus_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'basCus.name',
      key: 'a.cus_code',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('核发店面'),
      dataIndex: 'hfdmName',
      key: 'a.hfdm_code',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('是否首批'),
      dataIndex: 'bsp',
      key: 'a.bsp',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'bus_is_first',
    },
    {
      title: t('物流公司'),
      dataIndex: 'wlComp',
      key: 'a.wl_comp',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物流单号'),
      dataIndex: 'wlNo',
      key: 'a.wl_no',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('余额'),
      dataIndex: 'zhMoney',
      key: 'a.zh_money',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('促销费用余额'),
      dataIndex: 'cxMoney',
      key: 'a.cx_money',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('经销商返利余额'),
      dataIndex: 'flMoney',
      key: 'a.fl_money',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('使用账户金额'),
      dataIndex: 'useZhMoney',
      key: 'a.use_Zh_money',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('使用返利金额'),
      dataIndex: 'useFlMoney',
      key: 'a.use_fl_money',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('使用促销金额'),
      dataIndex: 'useCxMoney',
      key: 'a.use_cx_money',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('审核人'),
      dataIndex: 'auditor.userName',
      key: 'a.auditor',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('审核日期'),
      dataIndex: 'auditDate',
      key: 'a.audit_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('更新日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('退回理由'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑退货申请单'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'shop:apply:soReturnApply:edit',
        ifShow: record.auditStatus != '1',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除退货申请单'),
        popConfirm: {
          title: t('是否确认删除退货申请单'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'shop:apply:soReturnApply:edit',
        ifShow: record.auditStatus != '1',
      },
    ],
  };

  const [registerModal, { openModal, closeModal }] = useModal();
  const [registerForm, { resetFields, validate }] = useForm({
    labelWidth: 80,
    schemas: [
      {
        label: t('客户'),
        field: 'cusCode',
        fieldLabel: 'basCus.name',
        component: 'TreeSelect',
        componentProps: {
          api: basUserCusTreeData,
          canSelectParent: false,
          allowClear: true,
          immediate: true,
        },
        required: true,
        colProps: { lg: 24, md: 24 },
        itemProps: { validateTrigger: 'blur' },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: soReturnApplyListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await soReturnApplyDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 审核
  async function handleAudit() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await auditOrder({ selIds: selIds.value });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }

  async function handleApply() {
    // openDrawer3(true, record);
    await openModal(true, {});
    resetFields();
  }

  async function handleModal() {
    let data = await validate();
    openDrawer(true, data);
    closeModal();
  }

  async function handleUnSp() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await batchDeSp({ selIds: selIds.value });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }
</script>
